package com.example.demo.service.impl;

import com.example.demo.context.BaseContext;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.ShoppingCartMapper;
import com.example.demo.pojo.dto.ShoppingCartAddDTO;
import com.example.demo.pojo.entity.Goods;
import com.example.demo.pojo.entity.ShoppingCart;
import com.example.demo.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public List<ShoppingCart> list() {
        Long userId= BaseContext.getCurrentId();
        ShoppingCart shoppingCart=ShoppingCart.builder()
                .userId(userId)
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void select(Long id) {
        ShoppingCart shoppingCart=shoppingCartMapper.getById(id);
        shoppingCart.setIsSelect(Math.abs(shoppingCart.getIsSelect()-1));
        shoppingCartMapper.updateIsSelectById(shoppingCart);
    }

    @Override
    public void add(ShoppingCartAddDTO shoppingCartAddDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartAddDTO,shoppingCart);
        Long userId=BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if(list!=null&&list.size()==1){
            ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
        }else {
            Goods goods=goodsMapper.getById(shoppingCartAddDTO.getGoodsId());
            shoppingCart.setName(goods.getName());
            shoppingCart.setImage(goods.getImage());
            shoppingCart.setCategory(goods.getCategory());
            shoppingCart.setNumber(1);
            shoppingCart.setAmount(goods.getPrice());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    public void sub(ShoppingCartAddDTO shoppingCartAddDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartAddDTO,shoppingCart);
        Long userId=BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if(list!=null||list.size()>0){
            ShoppingCart cart=list.get(0);
            if(cart.getNumber()>1){
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }else{
                shoppingCartMapper.deleteById(cart);
            }
        }
    }


    public void cleanShoppingCart() {
        Long userId=BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userId);
    }
}
